import React from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends React.Component {
  render() {
    return (
      <>
        <div>home页面</div>
      </>
    );
  }
}

const About = () => {
  return <div>about页面</div>;
};

const Detail = (props) => {
  console.log(props);
  // location.search  "?id=234&name=zhangsan"

  // new URL();
  const urlObj = new URLSearchParams(props.location.search);
  console.log(urlObj.get("id"));

  return <div>详情页页面</div>;
};

const App = () => {
  return (
    <>
      <h2>react-router-v5-query传参</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail?id=234&name=zhangsan#abz">详情页</Link>
        </li>
        <li>
          <Link
            to={{
              pathname: "/detail",
              search: "?id=567&name=lisi",
              hash: "#abz",
              // 在url上面不可见的传参方式
              state: {
                x: 10,
                y: 20,
              },
            }}
          >
            详情页2
          </Link>
        </li>
      </ul>

      <hr />

      <Switch>
        <Route path="/home" component={Home}></Route>
        <Route path="/about" component={About}></Route>
        <Route path="/detail" component={Detail}></Route>
      </Switch>
    </>
  );
};

export default App;
